//
//  AlertAction.m
//  JAlertDemo
//
//  Created by mac on 2019/3/14.
//  Copyright © 2019 mac. All rights reserved.
//

#import "AlertAction.h"

@interface AlertAction ()

@end
static AlertAction *instance = nil;
@implementation AlertAction

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[AlertAction alloc] init];
    });
    return instance;
}
- (void)showAlertSheetWithTitle:(NSString *)title message:(NSString *)message itemsArr:(NSArray *)itemsArr selectItemBlock:(void (^)(NSString *))selectItemBlock{
    
    self.selectBlock = selectItemBlock;
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    alertController.view.tintColor = [UIColor textBlackColor];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertController addAction:cancelAction];
    
    
    if (itemsArr.count == 0) {
        return;
    }
    for (int i = 0; i < itemsArr.count; i++) {
        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:itemsArr[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (self.selectBlock) {
                self.selectBlock(itemsArr[i]);
            }
        }];
        [alertController addAction:alertAction];
    }
}

- (void)showAlertWithTitle:(NSString *)title message:(NSString *)message confirmBtnTitle:(NSString *)confirmBtnTitle confirmBlock:(void (^)(void))confirmBlock cancelBlock:(void (^)(void))cancelBlock{
    self.confirmBlock = confirmBlock;

    if ([JTool iSNull:title]) {
        title = @"";
    }
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    alertController.view.tintColor = [UIColor textBlackColor];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
    
    @weakify(self);
    UIAlertAction *confirmBtn = [UIAlertAction actionWithTitle:confirmBtnTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        @strongify(self);
        if (self.confirmBlock) {
            self.confirmBlock();
        }
    }];
    [confirmBtn setValue:[UIColor primaryColor] forKey:@"_titleTextColor"];
    [alertController addAction:confirmBtn];
    
    UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        @strongify(self);
        if (self.cancelBlock) {
            self.cancelBlock();
        }
    }];

    [alertController addAction:cancelBtn];
}
@end
